#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "JEU.h"

void jeInit_Jeu(JEU* je)
{
    je->de=deCreer_De();
    je->pi=piCreer_Pion();
    je->ta=caCreer_Case();
    je->tabcarte=carCreer_Carte();
    je->avancement=0;
    je->nbjoueur=0;
    je->d = 0;
    je->resped=0;
    je->resmail=0;
}

void jeRecup_Des(JEU* je)
{
    int de;
    assert (je!=NULL);
    deTirage_Alea(je->de);
    de = je->de->de1 + je->de->de2;
    je->d = de;
}

void jeDeplacement_PionVoiture(JEU* je)
{
    if(je->pi->numcase>39)
    {
        je->pi->numcase = 0;
    }
        printf("le pion est sur la case %d \n",je->pi->numcase);
        je->pi->numcase = je->pi->numcase + je->d;
        je->pi->x_depVoiture = je->ta[je->pi->numcase].x;
        je->pi->y_depVoiture = je->ta[je->pi->numcase].y;
        printf("le pion est sur la case de coordonner x %f \n",je->pi->x_depVoiture);
        printf("le pion est sur la case de coordonner y %f \n",je->pi->y_depVoiture);
}

void jeDeplacement_PionTram(JEU* je)
{
    if(je->pi->numcase>39)
    {
        je->pi->numcase = 0;
    }
        printf("le pion est sur la case %d \n",je->pi->numcase);
        je->pi->numcase = je->pi->numcase + je->d;
        je->pi->x_depTram = je->ta[je->pi->numcase].x;
        je->pi->y_depTram = je->ta[je->pi->numcase].y;
        printf("le pion est sur la case de coordonner x %f \n",je->pi->x_depTram);
        printf("le pion est sur la case de coordonner y %f \n",je->pi->y_depTram);
}

void jeDeplacement_PionBus(JEU* je)
{
    if(je->pi->numcase>39)
    {
        je->pi->numcase = 0;
    }
        printf("le pion est sur la case %d \n",je->pi->numcase);
        je->pi->numcase = je->pi->numcase + je->d;
        je->pi->x_depBus = je->ta[je->pi->numcase].x;
        je->pi->y_depBus = je->ta[je->pi->numcase].y;
        printf("le pion est sur la case de coordonner x %f \n",je->pi->x_depBus);
        printf("le pion est sur la case de coordonner y %f \n",je->pi->y_depBus);
}

void jeDeplacement_PionVelo(JEU* je)
{
    if(je->pi->numcase>39)
    {
        je->pi->numcase = 0;
    }
        printf("le pion est sur la case %d \n",je->pi->numcase);
        je->pi->numcase = je->pi->numcase + je->d;
        je->pi->x_depVelo = je->ta[je->pi->numcase].x;
        je->pi->y_depVelo = je->ta[je->pi->numcase].y;
        printf("le pion est sur la case de coordonner x %f \n",je->pi->x_depVelo);
        printf("le pion est sur la case de coordonner y %f \n",je->pi->y_depVelo);
}


void jeActualise_Note(JEU* je, char c)
{
    if (je->ca->proprietaire != NULL)
    {
        if(je->ca->proprietaire != je->pi->jo->nom)
        {
            je->pi->jo->note = je->pi->jo->note - je->ca->prix_a_payer;
            strcpy(je->pi->jo->nom , je->ca->proprietaire);
            je->pi->jo->note = je->pi->jo->note + je->ca->prix_a_payer;
        }
    }
    else
    {
        printf("Voulez vous acheter la carte : O pour oui et N pour non\n");
        scanf("%s", &c);
        if (c=='O')
        {
            je->pi->jo->note = je->pi->jo->note - je->ca->prix_achat;
            strcpy(je->ca->proprietaire, je->pi->jo->nom);
        }

    }
}

void jeTirage_Carte(JEU* je)
{
     int i;
     int res_mail=(rand() % (12) + 1);
     int res_refped=(rand() % (24) + 13);

     if (strcmp(je->ta->couleur, "MAIL")==0)
     {
        if (res_mail==1)
        {
            je->pi->jo->note = je->pi->jo->note - 7;
        }
        else if (res_mail==2)
        {
            je->pi->jo->note = je->pi->jo->note - 7;
        }
        /*else if (res_mail==3)
        {
            je->pi->jo->note = je->pi->jo->note + 5;
            je->pi->x_dep = 2;
            je->pi->y_dep = 1;
        }*/
        else if (res_mail==4)
        {
            je->pi->jo->note = je->pi->jo->note - 10;
        }
        else if (res_mail==5)
        {
            strcpy(je->tabcarte[Mcar5].proprietaire,je->pi->jo->nom);
        }
        else if (res_mail==6)
        {
            je->pi->numcase = 11;
        }
        else if (res_mail==7)
        {
            je->pi->jo->note = je->pi->jo->note + 2;
        }
        else if (res_mail==8)
        {
            je->pi->numcase = je->pi->numcase - 3;
        }
        else if (res_mail==9)
        {
            je->pi->jo->note = je->pi->jo->note - 3;
        }
        else if (res_mail==10)
        {
            je->pi->numcase = 40;
        }
        else if (res_mail==11)
        {
            je->pi->numcase = 24;
        }
        else if (res_mail==12)
        {
            je->pi->numcase = 35;
        }
     }

     if (strcmp(je->ta->couleur,"Referent Pedagogique")==0)
     {
        if (res_refped==13)
        {
         je->pi->jo->note = je->pi->jo->note + 2;
        }
        else if (res_refped==14)
        {
            res_mail=(rand() % (12)) + 1;
            if (res_mail==1)
            {
                je->pi->jo->note = je->pi->jo->note - 7;
            }
            else if (res_mail==2)
            {
                je->pi->jo->note = je->pi->jo->note - 7;
            }
           /* else if (res_mail==3)
            {
                je->pi->jo->note = je->pi->jo->note + 5;
                je->pi->x_dep = 2;
                je->pi->y_dep = 1;
            }*/
            else if (res_mail==4)
            {
                je->pi->jo->note = je->pi->jo->note - 10;
            }
            else if (res_mail==5)
            {
                strcpy(je->tabcarte[Mcar5].proprietaire,je->pi->jo->nom);
            }
            else if (res_mail==6)
            {
                je->pi->numcase = 11;
            }
            else if (res_mail==7)
            {
                je->pi->jo->note = je->pi->jo->note + 2;
            }
            else if (res_mail==8)
            {
                je->pi->numcase = je->pi->numcase - 3;
            }
            else if (res_mail==9)
            {
                je->pi->jo->note = je->pi->jo->note - 3;
            }
            else if (res_mail==10)
            {
                je->pi->numcase = 40;
            }
            else if (res_mail==11)
            {
                je->pi->numcase = 24;
            }
            else if (res_mail==12)
            {
                je->pi->numcase = 35;
            }
        }
        /*else if (res_refped==15)
        {
            je->pi->jo->note = je->pi->jo->note + 5;
            je->pi->x_dep = 2;
            je->pi->y_dep = 1;
        }*/
        else if (res_refped==16)
        {
            je->pi->jo->note = je->pi->jo->note - 2;
        }
        else if (res_refped==17)
        {
            strcpy(je->tabcarte[RPcar5].proprietaire,je->pi->jo->nom);
        }
        else if (res_refped==18)
        {
            je->pi->numcase = 11;
        }
        else if (res_refped==19)
        {
            je->pi->jo->note = je->pi->jo->note - 10;
        }
        else if (res_refped==20)
        {
            for(i=0;i<je->nbjoueur;i++)
            {
                if (je->pi->jo[i].nom!=je->pi->jo->nom)
                {
                    je->pi->jo[i].note=je->pi->jo[i].note - 2;
                }
            }
            je->pi->jo->note= je->pi->jo->note+ (2*je->nbjoueur);

        }
        else if (res_refped==21)
        {
            je->pi->jo->note = je->pi->jo->note + 4;
        }
        else if (res_refped==22)
        {
           je->pi->jo->note = je->pi->jo->note + 3;
        }
        else if (res_refped==23)
        {
            res_refped=11;
        }
        else if (res_refped==24)
        {
            je->pi->jo->note = je->pi->jo->note + 2;
        }
     }
     je->resped=res_refped;
     je->resmail=res_mail;
}

void jeAmelioration(JEU* je)
{
    TABCASE ta = je->ta;
    int j;
    int i=0;
    char c;
    for (j=je->pi->numcase-3; j<je->pi->numcase+3;j++)
    {
        if(strcmp(ta[j].couleur,ta[je->pi->numcase].couleur)==1)
        {
            if(strcmp(ta[j].proprietaire,ta[je->pi->numcase].proprietaire)==1)
            {
                i=i+1;
            }
        }
    }
    if ((strcmp(ta[je->pi->numcase].couleur, "bleu foncé")==1)  || (strcmp(ta[je->pi->numcase].couleur,"violet")==1))
    {
        if (i==2)
        {
            printf("Voulez vous améliorer votre case : O pour oui N pour non\n");
            scanf("%s /n", &c);
            if (c=='O')
            {
                if(je->pi->jo->note>=je->pi->jo->note-(ta[je->pi->numcase].prix_achat*i))
                {
                    ta[je->pi->numcase].niveau ++;
                    je->pi->jo->note = je->pi->jo->note-(ta[je->pi->numcase].prix_achat*i);
                    for (j=je->pi->numcase-3; j<je->pi->numcase+3;j++)
                    {
                        if(ta[j].couleur==ta[je->pi->numcase].couleur)
                        {
                            ta[je->pi->numcase].prix_a_payer=ta[je->pi->numcase].prix_a_payer+(ta[je->pi->numcase].prix_a_payer*(ta[je->pi->numcase].niveau/3));
                        }
                    }
                }
                else printf("Vous n'avez pas assez de points\n");
            }
        }
    }
    if (i==3)
    {
        printf("Voulez vous améliorer votre case : O pour oui N pour non\n");
        scanf("%s /n", &c);
        if (c=='O')
        {
            if(je->pi->jo->note>=je->pi->jo->note-(ta[je->pi->numcase].prix_achat*i))
            {
                ta[je->pi->numcase].niveau ++;
                je->pi->jo->note = je->pi->jo->note-(ta[je->pi->numcase].prix_achat*i);
                for (j=je->pi->numcase-3; j<je->pi->numcase+3;j++)
                {
                    if(ta[j].couleur==ta[je->pi->numcase].couleur)
                    {
                        ta[je->pi->numcase].prix_a_payer=ta[je->pi->numcase].prix_a_payer+(ta[je->pi->numcase].prix_a_payer*(ta[je->pi->numcase].niveau/3));
                    }
                }
            }
            else printf("Vous n'avez pas assez de points\n");
        }
    }
}

void jeTest_Jeu()
{

    JEU je;
    int j1,j2;
    int note;
    int res_mail=(rand() % (12)) + 1;
    int res_refped=(rand() % (24)) + 13;
    char c = 'N';
    jeInit_Jeu(&je);
    printf("Tests du module jeu : \n\n") ;
    /* Début du test de jeInit_Jeu */
    printf("Test de jeInit_Jeu ... \n") ;
    assert(je.avancement==0);
    assert(je.nbjoueur==0);

    /* Début du test de jeRecup_Des */
    printf("Test de jeRecup_Des ... \n") ;
    jeRecup_Des(&je);
    assert(je.d != 0);

    /* Début du test de jeDeplacement_Pion */
    printf("Test de jeDeplacement_Pion ... \n") ;
    j1=je.pi->x_depTram;
    j2=je.pi->y_depTram;
    jeDeplacement_PionTram(&je);
    assert(j1!=je.pi->x_depTram || j2!=je.pi->y_depTram);

    /* Début du test de jeActualise_Note */
    printf("Test de jeActualise_Note ... \n") ;
    note=je.pi->jo->note;
    jeActualise_Note(&je,c);
    if (je.ca->proprietaire != NULL)
    {
        if(je.ca->proprietaire != je.pi->jo->nom)
        {
            assert(note!=je.pi->jo->note);
        }
        else
        {
            assert(note==je.pi->jo->note);
        }
    }
    if (je.ca->proprietaire != NULL)
    {
        if(note!=je.pi->jo->note)
        {
            assert(strcmp(je.ca->proprietaire, je.pi->jo->nom)==1);
        }
        else
        {
            assert(strcmp(je.ca->proprietaire, je.pi->jo->nom)==0);
        }
    }

    if (c=='O')
    {
        assert(note!=je.pi->jo->note);
        assert(strcmp(je.ca->proprietaire, je.pi->jo->nom)==0);
    }
    else
    {
        assert(note==je.pi->jo->note);
        assert(strcmp(je.ca->proprietaire, je.pi->jo->nom)==1);
    }

    /* Début du test de carTirage_Carte */
    printf("Test de carTirage_Carte ... \n");
    /*carTirage_Carte(&je);*/
    assert(res_mail>0 && res_mail<13);
    assert(res_refped>12 && res_mail<25);

}

